cuda.core: make buffer cleanup context-safe - #2455
Conversation
Ensure deferred and MR-backed buffer destruction activates the allocation or deallocation-stream context, preventing invalid-context leaks after callers clear their current context.
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
|
@Andy-Jost is there any performance concerns here? I vaguely remember some microbenchmarking you did but I don't recall if it was related to this PR. |
@kkraus14 I think you're referring to the discussion in #2311, which showed ~9ns for a current-context check and ~37ns for a check-and-switch. That came up when discussing |
|
/ok to test |
|
Restore fixture-managed contexts in direct cleanup tests and run deferred pending-call cleanup in a subprocess so parallel workers cannot leak thread-local state.
|
/ok to test |
Resolve allocation and stream contexts at the Buffer boundary so the C++ resource layer only retains and restores explicit context handles.
|
/ok to test |
The approach here is a similar pattern, yes? If so I think we're good. If not I think it would be good to do a minimal test just to ensure we aren't adding 100s of ns of overhead. |
• Separates allocation ownership from release-stream context. • Handles legacy default/PTDS context binding without hidden synchronization. • Adds explicit MR adoption streams and strict context-independent pointer checks. • Extends native pool, async, IPC, graphics, and synchronous cleanup paths. • Makes cleanup failures visible. Validation: • Focused GPU tests: 7 passed. • Full memory/graph suites: 621 passed, 30 skipped. • Pre-commit: passed. • No compiler/Cython warnings; 58 existing performance hints. • GPU build reported existing package-version resolver conflicts.
|
/ok to test |
|
/ok to test |
Retain and restore the CUDA context required by deferred buffer cleanup so destruction does not depend on ambient thread state.
|
/ok to test |
Summary
Make device-pointer cleanup retain and activate the CUDA context required by the allocation or its deallocation stream. This prevents deferred graph attachment cleanup and ordinary
Bufferdestruction from leaking memory when no context is current on the cleanup thread.Changes
Stream_resolve_context) and pass an explicit stream/context pair into device-pointer resource handles; C++ stores that pair and makes the context current during destruction.MemoryResource.deallocate()callbacks, then restore the caller's prior context.CUDA_ERROR_INVALID_CONTEXTas successful memory-pool deallocation, and report cleanup failures onstderrinstead of discarding them.stream=argument toBuffer.from_handleandManagedBuffer.from_handle, defaulting to the calling thread's default stream when a memory resource owns the pointer.Review guide
_stream.pyx/_buffer.pyx/_ipc.pyxfor how Cython chooses the release context.resource_handles.cpp/.hppfor storing that context as a handle dependency and restoring it during teardown.Related Work
from_handle/from_*APIs #1989.